<?php
namespace app\admin\controller;
use think\Db;
class Member extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['user_level'] = $role;
            }
            if(isset($key)&&$key!="")
            {
                $map['user_username|user_phone'] = ['like',"%" . $key . "%"];
            }
            if(session('area')){
                $map['user_area'] = ['in',session ('area')];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('u_user u')
                ->join('area a','u.user_area = a.district_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('u_user u')
                ->join('area a','u.user_area = a.district_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('user_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(session('area')){
            $show = false;
        }else{
            $show = true;
        }
        $this->assign('show',$show);
        return $this->fetch ("member/index");
    }


    public function memberStatus(){
        extract (input());
        if($num == 2){
            $msg = '禁用';
        }else{
            $msg = '启用';
        }
        $res = Db::name('u_user')->where(['user_id'=>$id])->setField ('user_status',$num);
        if(!$res){
            return json(['code'=>100,'msg'=> $msg.'失败']);
        }
    }

    public function memberEdit(){
        extract (input());
        if(request ()->isPost ()){
            $qu = Db::name('area')->where('district_id',$area)->find();
            $shi = Db::name('area')->where('district_id',$qu['pid'])->find();
            $res = Db::name('u_user')->where('user_id',$id)->update(['user_province'=>$shi['pid'],'user_city'=>$qu['pid'],'user_area'=>$area]);
            if($res){
                $have = Db::name('partner')->where('par_user_id',$id)->field('par_qu_id')->find();
                if($have){
                    Db::name('partner')->where('par_user_id',$id)->setField('par_qu_id',$area);
                }
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $area = Db::name('area')->where('level',3)->select ();
        return $this->fetch ('member/member_edit',['area'=>$area,'id'=>$id]);
    }


    public function cityMan(){
        extract (input());
        $user = Db::name('u_user')->where('user_id',$id)->find();
        Db::name('u_user')->where('user_area',$user['user_area'])->setField ('user_city_man',1);
        $res = Db::name('u_user')->where('user_id',$id)->setField ('user_city_man',2);
         if($num == 1){
             $msg = '禁用';
         }else{
             $msg = '启用';
         }
        if(!$res){
            return json(['code'=>100,'msg'=> $msg.'失败']);
        }
    }

    public function addSort(){
        extract(input());
        $user = Db::name('u_user')->where('user_id',$id)->find();
        Db::startTrans();
        try{
            Db::name('u_user')->where('user_id',$id)->setInc('user_integral',$value);
            if($user['user_area']){
                Db::name('admin')->where('area',$user['user_area'])->setDec('integral',$value);
            }else{
                Db::name('admin')->where('id',1)->setDec('integral',$value);
            }
            Db::commit();
            return json(['code'=>200,'msg'=>'排序成功']);
        }catch (\Exception $e){
            Db::rollback();
            return json(['code'=>100,'msg'=>'排序失败']);
        }
    }

    // 会员积分来源详情
    public function memberDetail(){
        if(request()->isAjax ()){
            extract(input());
            if(isset($key)&&$key!="")
            {
                $map['consume_content'] = ['like',"%" . $key . "%"];
            }
            $map['consume_user_id'] = $user_id;
            $Nowpage    = input('page') ? input('page'):1;
            $limits     = input("limit")?input("limit"):10;// 获取总条数;
            $count  = Db::name('jifen_consume')
                    ->where($map)
                    ->count ();
            $list   = Db::name('jifen_consume')
                    ->where($map)
                    ->page($Nowpage,$limits)
                    ->order('consume_add_time desc')
                    ->select ();
            foreach ($list as $key => $vo) {
                $list[$key]['user_username'] = DB::name('u_user')->where('user_id',$user_id)->value('user_username');
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }else{
            extract(input());
            $this->assign('user_id',$user_id);
            return view();
        }
    }
}